<?php

namespace fast;

use think\Db;

use think\Exception;
use fast\Http;
use fast\WechatMini;


/**
 * Http 请求类
 */
class Fahuo
{

  
       
    public static function addFahuo($id)
    {
        



        $order = Db::table('fa_pifa_order')->find($id);



        if (empty($order)) {
            logs('order_tongbu','订单ID----'.$id.'-----订单不存在');     
            return;
        }

        $map_user['user_id'] = $order['user_id'];
        $user = Db::table('fa_third')->where($map_user)->find();
        
      
        if (empty($user)) {
            logs('order_tongbu','订单ID----'.$id.'-----user不存在');     
            return;
        }
        $wechat = new WechatMini();



        $map2['wx_order'] = $order['transactionid'];


        $orderliushui = Db::table('fa_pifa_orderliushui')->where($map2)->find();

     
        
        $token  = $wechat->token;

      
  
        
        $url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=".$token;

        $data['order_key']['order_number_type'] = 2;
        $data['order_key']['mchid'] = 1718987852;
        $data['order_key']['transaction_id'] = $order['transactionid'];
        // $data['order_key']['out_trade_no'] = $orderliushui['order_no'];
        
        $data['shipping_list'] = [];



        $map3['order_id'] = $id;
        $OrderitemModel = new \app\admin\model\pifa\Orderitem;

        $order_item = $OrderitemModel::with(['goodsitem'])->where($map3)->select();



        if (empty($order_item)) {
           logs('wechat_order','订单ID----'.$id.'-----商品没查到');     
           return;
        }
      

        foreach ($order_item as $key => $value) {
            $data['shipping_list'][$key]['item_desc'] = $value['goods_name'];//商品编码
            break;
        }

        $data['delivery_mode'] = 1;
        $data['logistics_type'] = 2;
        $data['payer'] = [];
        $data['upload_time'] = date("c", time());
        $data['payer']['openid'] = $user['openid'];


        $sysParams = json_encode($data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

     

     
        $headers = ['Content-type: application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        $response = Http::sendRequest($url, $sysParams, 'POST', $options);
        logs('wechat_order','发货信息_______'.$sysParams);
        logs('wechat_order','订单返回_______'.json_encode($response,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
     

    }
    




}
